package com.combetter.changxing.db;

import android.arch.persistence.db.SupportSQLiteDatabase;
import android.arch.persistence.room.Database;
import android.arch.persistence.room.Room;
import android.arch.persistence.room.RoomDatabase;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.VisibleForTesting;

import com.combetter.changxing.AppExecutors;
import com.combetter.changxing.db.dao.CollectDao;
import com.combetter.changxing.db.dao.FacilityDao;
import com.combetter.changxing.db.dao.RecordDao;
import com.combetter.changxing.db.dao.UpLoadFacilityDao;
import com.combetter.changxing.db.dao.UserDao;
import com.combetter.changxing.db.entity.Collect;
import com.combetter.changxing.db.entity.Facility;
import com.combetter.changxing.db.entity.Record;
import com.combetter.changxing.db.entity.UpLoadFacility;
import com.combetter.changxing.db.entity.User;
import com.combetter.changxing.util.LogUtil;

/**
 * Created by Mark.fan on 2018/6/1.
 */
@Database(entities = {User.class, Facility.class, UpLoadFacility.class, Collect.class, Record.class}, version = 1, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {

    private static AppDatabase sInstance;

    public abstract UserDao userDao();
    public abstract RecordDao recordDao();

    public abstract FacilityDao facilityDao();

    public abstract UpLoadFacilityDao uploadFacilityDao();

    public abstract CollectDao collectDao();

    @VisibleForTesting
    public static final String DATABASE_NAME = "electronicPipe.db";

    public static AppDatabase getInstance(final Context context, final AppExecutors executors) {
        if (sInstance == null) {
            sInstance = buildDatabase(context, executors);
            sInstance.updateDatabaseCreated(context);
        }
        return sInstance;
    }

    private static AppDatabase buildDatabase(final Context context, final AppExecutors executors) {
        return Room.databaseBuilder(context, AppDatabase.class, DATABASE_NAME).fallbackToDestructiveMigration()
                .addCallback(new Callback() {
                    @Override
                    public void onCreate(@NonNull SupportSQLiteDatabase db) {
                        super.onCreate(db);
                        LogUtil.e(DATABASE_NAME + ", 创建回调中！");
                        executors.diskIO().execute(() -> {
                            AppDatabase database = AppDatabase.getInstance(context, executors);
                        });
                        //创建完数据库之后回调
                    }
                })
                .allowMainThreadQueries()
                .build();
    }

    private static void addDelay() {
        try {
            Thread.sleep(4000);
        } catch (InterruptedException ignored) {
        }
    }

    /**
     * Check whether the database already exists and expose it via
     */
    private void updateDatabaseCreated(final Context context) {
        if (context.getDatabasePath(DATABASE_NAME).exists()) {
            LogUtil.e("database" + DATABASE_NAME + " 已存在");
        } else {
            LogUtil.e("database " + DATABASE_NAME + "不存在");
        }
    }
}
